package org.game.plugin.event;

import java.util.List;
import java.util.Map;

import org.cfg.EventConditionCfg;
import org.cfg.MultiConditionEventCfg;
import org.cfg.cache.EventConditionCfgCache;
import org.cfg.cache.MultiConditionEventCfgCache;
import org.constant.EventConditionKey;
import org.constant.TriggerEventResult;
import org.constant.smallkey.WorkLogSmallKey;
import org.game.db.bean.Building;
import org.game.db.bean.Player;
import org.game.db.bean.Role;
import org.game.db.cache.BuildingCache;
import org.game.db.cache.RoleCache;
import org.game.manager.DatingManager;
import org.game.manager.ItemPackageHelper;
import org.game.manager.RoleManager;
import org.game.module.datingbo.DatingBO;
import org.game.module.rolebo.RoleBO;
import org.game.msgBuilder.BuildingMsgBuilder;
import org.game.msgBuilder.DatingMsgBuilder;
import org.game.msgBuilder.RoleMsgBuilder;
import org.game.protobuf.c2s.C2SDatingMsg;
import org.utils.CommonUtil;

import com.google.common.collect.Maps;
import com.sojoys.artifact.tools.ToolDate;
import com.sojoys.artifact.tools.ToolMap;
import com.sojoys.artifact.tools.ToolRandom;

/**
 * 事件处理器
 * @author SandKing
 *
 */
public interface GameEventHandler {
	void onEvent(GameEventListener listener,GameEvent event);

	/**
	 * 校验事件进度并修改事件状态
	 */
	default void handler(GameEventListener listener, GameEvent event, GameEventVector vector, int conditionId, Map params, int maxProcess) {
		
		EventConditionCfg eventConditionCfg = EventConditionCfgCache.me().getById(conditionId);
		if (eventConditionCfg == null) {
			return;
		}
		int eventId = eventConditionCfg.getEventId();
		
		// 检查该条件监听的事件
		if(event.getEventType()!=eventId){
			return;
		}

		// 如果条件参数为空，则任务成功触发条件
		boolean trigger = (params == null || params.isEmpty());
		
		// 当前进度
		int nowProgress  = vector.getProgress();
		if (nowProgress >= maxProcess) {
			trigger = true;
		}else {
			trigger = false;
		}
		
		event.push(new GameEventStack(eventId,conditionId, params, nowProgress,maxProcess, trigger));
		listener.onEvent(event);
		GameEventStack eventStack = event.peek();
		
		// 条件触发成功
		if(eventStack.isTrigger(vector,eventStack.getProgress())){
			vector.trigger(maxProcess);
			vector.notify(event.getPlayer());				
		}
	}

	/**
	 * 事件结果处理
	 */
	default void eventResult(GameEvent event,Map result,Object obj){
		int eventResultType = ToolMap.getInt(EventConditionKey.EVENT_RESULT_TYPE, result);
		Map data = ToolMap.getMap(EventConditionKey.EVENT_RESULT_DATA, result);
		Player player = event.getPlayer();
		switch (eventResultType) {
			//1.解锁礼物
			case TriggerEventResult.UNBLOCKING_GIFT: {
				int roleCid = ToolMap.getInt(EventConditionKey.ROLE_CID, data);
				int giftCid = ToolMap.getInt(EventConditionKey.GIFT_CID, data);
				RoleManager.unlockGift(player, roleCid, giftCid);
				break;
			}
			//3.约会
			case TriggerEventResult.DATING: {
				int scriptCid = ToolMap.getInt(EventConditionKey.SCRIPT_ID, data);
				int roleCid = ToolMap.getInt(EventConditionKey.ROLE_CID, data);
				Role role = RoleCache.me().getByCidPlayerId(roleCid,player.getId());
				role.getRoleProxy().addTriggerDating(scriptCid);
				player.getPlayerProxy().sendMsg(DatingBO.updateTriggerDating, DatingMsgBuilder.createUpdateTriggerDatingMsg(role));
				break;
			}
			//4.打工奖励
			case TriggerEventResult.WORK:{
//				int cfgId = ToolMap.getInt(EventConditionKey.MULTI_CONDITION_EVENT_CID, data);
				MultiConditionEventCfg cfg = (MultiConditionEventCfg) obj;
				if (cfg != null){
					int buildCid = ToolMap.getInt(EventConditionKey.BUILD_CID, event.getIn());
					Building building = BuildingCache.me().getByPlayerIdCid(player.getId(), buildCid);

					List logs = building.getEventLogs();
					// 奖励
					Map<Integer, Integer> rewards = Maps.newLinkedHashMap();
					ItemPackageHelper.unpack(data, null, rewards);

					Map<Integer, Object> log = Maps.newHashMap();
					log.put(WorkLogSmallKey.CID, cfg.getId());
					log.put(WorkLogSmallKey.TYPE, 1);
					log.put(WorkLogSmallKey.TIME, (int) (System.currentTimeMillis() / ToolDate.TIME_SECOND));
					int textLength = cfg.getDescribe().length;
					if (textLength <= 1) {
						log.put(WorkLogSmallKey.INDEX, 1);
					}else {
						log.put(WorkLogSmallKey.INDEX, ToolRandom.randInt(0, textLength-1)+1);
					}

					CommonUtil.changeMap(building.getReward(), rewards);

					log.put(WorkLogSmallKey.REWARDS, rewards);
					logs.add(log);
					building.update();

					BuildingMsgBuilder.pushWorkLog(player, buildCid, log);
				}
				break;
			}
			default:
				break;
		}
	}
}
